/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.photop.sap.contactme.frame;

import cn.com.photop.sap.contactme.component.MyJTextField;
import cn.com.photop.sap.contactme.common.CommonConstant;
import cn.com.photop.sap.contactme.common.CommonSession;
import cn.com.photop.sap.contactme.model.ContactInfo;
import cn.com.photop.sap.contactme.service.CommonPropertiesService;
import cn.com.photop.sap.contactme.service.GetLocalContactInfoService;
import cn.com.photop.sap.contactme.util.Configuration;
import cn.com.photop.sap.contactme.util.StringUtil;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author Jiancheng.Zheng
 */
public class Main4TXTDataJFrame extends javax.swing.JFrame {
    //所有的集合 用于树形的显示
    private Map< String, Map<String, List<ContactInfo>>> mapContactInfo;
    //所有人员的List
    private List<ContactInfo> listContactInfo;
    //公司的集合
    private List<String> listCompany;
    //部门的集合 key 是公司
    private Map<String, List<String>> mapDepartment;

    /**
     * Creates new form MainJFrame
     */
    public Main4TXTDataJFrame() throws FileNotFoundException, IOException, Exception {

        listContactInfo = new ArrayList<ContactInfo>();
        //设置通讯录对象内容
        GetLocalContactInfoService glcis = new GetLocalContactInfoService();
        //所有的集合 用于树形的显示
        mapContactInfo = glcis.mapContactInfo;
        //所有人员的List
        listContactInfo = glcis.listContactInfo;
        //公司的集合
        listCompany = glcis.listCompany;
        listCompany.add(0, CommonSession.getGroupName());

        //部门的集合 key 是公司
        mapDepartment = glcis.mapDepartment;

        initComponents();

        this.setLocationRelativeTo(null);
        //初始化JTable1的右键菜单
        initJTablePopupMenu();


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        DefaultMutableTreeNode root = new DefaultMutableTreeNode( CommonSession.getGroupName() );
        Iterator it = mapContactInfo.entrySet().iterator();
        while (it.hasNext()) {
            Entry e = (Entry) it.next();
            String key = (String) e.getKey();
            DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(key);
            Map<String, List<ContactInfo>> m = (Map<String, List<ContactInfo>>) e.getValue();
            Iterator it1 = m.entrySet().iterator();
            while (it1.hasNext()) {
                Entry e1 = (Entry) it1.next();
                String key1 = (String) e1.getKey();
                DefaultMutableTreeNode child1_2 = new DefaultMutableTreeNode( key1 );
                child1.add(child1_2);
                ArrayList<ContactInfo> ciList = (ArrayList<ContactInfo>) e1.getValue();
                for (int i = 0; i < ciList.size(); i++) {
                    DefaultMutableTreeNode child1_2_3 = new DefaultMutableTreeNode( ciList.get(i).getName() );
                    child1_2.add(child1_2_3);
                }
            }

            root.add(child1);

        }
        DefaultTreeModel model = new DefaultTreeModel(root);
        jTree1 = new javax.swing.JTree();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable() {

            public String getToolTipText(MouseEvent e) {
                int row = jTable1.rowAtPoint(e.getPoint());
                int col = jTable1.columnAtPoint(e.getPoint());
                String tiptextString = null;
                if (row > -1 && col > -1) {
                    Object value = jTable1.getValueAt(row, col);
                    if (null != value && !"".equals(value)) {
                        tiptextString = value.toString();//悬浮显示单元格内容
                    }
                }
                return tiptextString;
            }
            //表格不允许被编辑
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        jLabel5 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("[未登陆]");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jLabel1.setText("姓名");

        jTree1 = new javax.swing.JTree(model);
        jTree1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTree1MouseClicked(evt);
            }
        });
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jTree1);

        jLabel2.setText("公司");

        final int size = listCompany.size();
        String[] jModel = (String[])listCompany.toArray( new String[size] );
        jComboBox1.setModel( new javax.swing.DefaultComboBoxModel( jModel ) );
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jComboBox1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jComboBox1PropertyChange(evt);
            }
        });
        jComboBox1.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
            public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                jComboBox1VetoableChange(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("宋体", 1, 30)); // NOI18N
        jLabel4.setText("II-VI通讯录");

        jComboBox2.insertItemAt(CommonConstant.CONSTANT_DEFAULT_DEPARTMENT, 0);
        jComboBox2.setSelectedIndex(0);

        jLabel3.setText("部门");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("搜索");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        //jTable1.setEnabled(false);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "分公司", "部门", "姓名", "直线", "内线", "传真", "手机", "邮箱", "备注"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable1MousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jLabel5.setText("姓名");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(96, 96, 96)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 66, Short.MAX_VALUE)
                        .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(104, 104, 104))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addContainerGap())))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(225, 225, 225)
                .addComponent(jLabel4)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jMenu1.setText("文件");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem4.setText("登陆管理");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem8.setText("公司管理");
        jMenu1.add(jMenuItem8);

        jMenuItem10.setText("部门管理");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem10);

        jMenuItem7.setText("人员管理");
        jMenu1.add(jMenuItem7);

        jMenuItem9.setText("数据刷新");
        jMenu1.add(jMenuItem9);
        jMenu1.add(jSeparator1);

        jMenuItem6.setText("退出软件");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("帮助");

        jMenuItem2.setText("数据更新");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuItem3.setText("软件更新");
        jMenu2.add(jMenuItem3);

        jMenuItem5.setText("查看Excel");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuItem1.setText("关于");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        AboutDialog aboutOkCancelDialog = new AboutDialog(null, true);
        aboutOkCancelDialog.show();        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        //sout
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
//        System.out.println( jComboBox1.getSelectedIndex() );
        String selectedItem = (String) jComboBox1.getSelectedItem();
        if (CommonSession.getGroupName().equals(selectedItem)) {
            jComboBox2.removeAllItems();
            jComboBox2.insertItemAt(CommonConstant.CONSTANT_DEFAULT_DEPARTMENT, 0);
            jComboBox2.setSelectedIndex(0);
            return;
        }
        //部门的集合 key 是公司
        List<String> listDepartment = mapDepartment.get(selectedItem);
//        listDepartment.add(0, "All");
        int size = listDepartment.size();
        if (size > 0) {
            String[] jModel = (String[]) listDepartment.toArray(new String[size]);
            jComboBox2.removeAllItems();
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(jModel));
            jComboBox2.insertItemAt(CommonConstant.CONSTANT_DEFAULT_DEPARTMENT, 0);
            jComboBox2.setSelectedIndex(0);
        } else {
            jComboBox2.removeAllItems();
            jComboBox2.insertItemAt(CommonConstant.CONSTANT_DEFAULT_DEPARTMENT, 0);
            jComboBox2.setSelectedIndex(0);
        }


    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jComboBox1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jComboBox1PropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1PropertyChange

    private void jComboBox1VetoableChange(java.beans.PropertyChangeEvent evt) throws java.beans.PropertyVetoException {//GEN-FIRST:event_jComboBox1VetoableChange
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1VetoableChange

    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jTree1ValueChanged

    /**
     * 搜索按钮触发的处理
     *
     * @param evt
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String company = jComboBox1.getSelectedItem().toString();
        String department = jComboBox2.getSelectedItem().toString();
        String name = jTextField1.getText();
//        System.out.println("Company:" + company + "|Department:" + department + "|Name:" + name);
        List<ContactInfo> ciList = new ArrayList<ContactInfo>();
        if (CommonSession.getGroupName().equals(company)) {
            //如果是集团 判断name 
            for (ContactInfo ci : listContactInfo) {
                //判断人名称
                if (StringUtil.isNotNull(name)) {
                    if (ci.getName().contains(name)) {
                        ciList.add(ci);
                    }
                } else {
                    ciList.add(ci);
                }
            }
        } else {
            Map<String, List<ContactInfo>> map1 = mapContactInfo.get(company);
            if (CommonConstant.CONSTANT_DEFAULT_DEPARTMENT.equals(department)) {
                //说明是All
                //遍历该公司的所有
                Iterator it = map1.entrySet().iterator();
                while (it.hasNext()) {
                    Entry e = (Entry) it.next();
                    List<ContactInfo> list1 = (List<ContactInfo>) e.getValue();
                    //判断人名称
                    for (ContactInfo ci : list1) {
                        //判断人名称
                        if (StringUtil.isNotNull(name)) {
                            if (ci.getName().contains(name)) {
                                ciList.add(ci);
                            }
                        } else {
                            ciList.add(ci);
                        }
                    }


                }

            } else {
                List<ContactInfo> list1 = map1.get(department);
                for (ContactInfo ci : list1) {
                    //判断人名称
                    if (StringUtil.isNotNull(name)) {
                        if (ci.getName().contains(name)) {
                            ciList.add(ci);
                        }
                    } else {
                        ciList.add(ci);
                    }
                }


            }
        }

        //设置JTable的值
        setJTableData(ciList);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * JTable 单击按钮
     *
     * @param evt
     */
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if (evt.getClickCount() == 2) {
//            jTable1.getSelectedRow();    
            int row = jTable1.rowAtPoint(evt.getPoint());
            int col = jTable1.columnAtPoint(evt.getPoint());
            int colNum = jTable1.getColumnCount();
            if (row > -1) {
                String company = (String) jTable1.getValueAt(row, 0);
                String department = (String) jTable1.getValueAt(row, 1);
                String name = (String) jTable1.getValueAt(row, 2);
                String tel = (String) jTable1.getValueAt(row, 3);
                String inside = (String) jTable1.getValueAt(row, 4);
                String fax = (String) jTable1.getValueAt(row, 5);
                String mobile = (String) jTable1.getValueAt(row, 6);
                String email = (String) jTable1.getValueAt(row, 7);
                String remark = (String) jTable1.getValueAt(row, 8);

                ContactInfo ci = new ContactInfo();
                if (StringUtil.isNotNull(company)) {
                    ci.setCompany(company);
                }
                if (StringUtil.isNotNull(department)) {
                    ci.setDepartment(department);
                }
                if (StringUtil.isNotNull(name)) {
                    ci.setName(name);
                }
                if (StringUtil.isNotNull(tel)) {
                    ci.setTel(tel);
                }
                if (StringUtil.isNotNull(inside)) {
                    ci.setInside(inside);
                }
                if (StringUtil.isNotNull(fax)) {
                    ci.setFax(fax);
                }
                if (StringUtil.isNotNull(mobile)) {
                    ci.setMobile(mobile);
                }
                if (StringUtil.isNotNull(email)) {
                    ci.setEmail(email);
                }
                if (StringUtil.isNotNull(remark)) {
                    ci.setRemark(remark);
                }

                //弹出框
                if (StringUtil.isNotNull(company)) {
                    DataDetailFrame ddf = new DataDetailFrame(ci);
                    ddf.setVisible(true);
                }

            }
//            for (int i = 0; i < colNum; i++) {
//                //循环获取值
////               jTable1.getValueAt( row, i );
//            }
//            
//            System.out.println("row:" + row + "|col:" + col);
//            if (row > -1 && col > -1) {
//                Object value = jTable1.getValueAt(row, col);
//                if (null != value && !"".equals(value)) {
//                }
//            }
        }
    }//GEN-LAST:event_jTable1MouseClicked
    private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
        //返回最后选择的节点
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
        //selectedNode.toString() 节点名称
        //selectedNode.getDepth() 节点深度
        //selectedNode.isLeaf() 是否是叶子
        //selectedNode.getParent().toString() 节点父类名称
        //集团名称：深度为3，否叶子
        //公司名称：深度为2，否叶子
        //部门名称：深度为1，否叶子
        //员工名称：深度为0，是叶子

        if (evt.getClickCount() == 2) {
            String department = new String();
            String company = new String();
            String name = new String();
            //获取选中的用户
            List<ContactInfo> listciTemp = new ArrayList<ContactInfo>();
            if (selectedNode.isLeaf()) {
                //如果是也只的话要获取相对应的部门，公司
                name = selectedNode.toString().trim();
                department = selectedNode.getParent().toString().trim();
                company = selectedNode.getParent().getParent().toString().trim();
                for (int i = 0; i < listContactInfo.size(); i++) {
                    ContactInfo ci = listContactInfo.get(i);
                    if (company.endsWith(ci.getCompany())
                            && department.endsWith(ci.getDepartment())
                            && name.endsWith(ci.getName())) {
                        listciTemp.add(ci);

                    }
                }
            } else {
                if (selectedNode.getDepth() == 1) {
                    //表示是部门
                    department = selectedNode.toString().trim();
                    company = selectedNode.getParent().toString().trim();
                    for (int i = 0; i < listContactInfo.size(); i++) {
                        ContactInfo ci = listContactInfo.get(i);
                        if (company.endsWith(ci.getCompany())
                                && department.endsWith(ci.getDepartment())) {
                            listciTemp.add(ci);

                        }
                    }
                } else if (selectedNode.getDepth() == 2) {
                    //表示是公司
                    company = selectedNode.toString().trim();
//                System.out.println("Company:"+company);
                    for (int i = 0; i < listContactInfo.size(); i++) {
                        ContactInfo ci = listContactInfo.get(i);
                        String companyTmp = ci.getCompany();
//                    System.out.println("companyTmp="+companyTmp );
                        if (company.endsWith(companyTmp)) {
                            listciTemp.add(ci);

                        }
                    }

                } else {
                    //标示集团用户
                    listciTemp = listContactInfo;
                }

            }
            //把获取到的listciTemp 设置到jTable1中去
            setJTableData(listciTemp);
        }
    }//GEN-LAST:event_jTree1MouseClicked

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        //打开Excel数据
        //获取配置文件
//        InputStream is = GetLocalContactInfoService.class.getResourceAsStream("/cn/com/photop/sap/contactme/config/common.properties");
//        Configuration rc = new Configuration(is);// 相对路径
//        String excelPath = rc.getValue("ExcelPath");
        String excelPath = CommonPropertiesService.getValue("ExcelPath");
        //获取本地文件的路径
        File file = new File(excelPath);
        try {
            Desktop.getDesktop().open(file);


        } catch (IOException ex) {
            Logger.getLogger(Main4TXTDataJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
    }//GEN-LAST:event_jMenu1ActionPerformed

    /**
     * 点击管理MENU按钮
     *
     * @param evt
     */
    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        if (CommonSession.user == null) {
            LogonJFrame lf = new LogonJFrame(this);
            lf.setVisible(true);
        } else {
            String message = "确定要退出管理";
            int res = javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,
                    message, "提示", javax.swing.JOptionPane.OK_CANCEL_OPTION);
//            System.out.println("res:" + res);
            if (res == 0) {
                //确定
                CommonSession.user = null;
                setJMenuItem4Text("登陆管理");
                this.setTitle("[未登陆]");
            }


        }


    }//GEN-LAST:event_jMenuItem4ActionPerformed

    /**
     * 退出软件
     *
     * @param evt
     */
    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    /**
     * JTABLE1 右键菜单的制作
     *
     * @param evt
     */
    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
//        System.out.println("isPopupTrigger：" + evt.isPopupTrigger());
//
//        if (evt.isAltDown() || evt.isPopupTrigger()) {
        // 取得右键点击所在行
//            int row = evt.getY() / jTable1.getRowHeight();
//            // 弹出菜单
//            jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());
//            /**
//             * 取得是表名的那一列
//             */
//            int tableNameColumn = -1;
//
//            for (int i = 0; i < jTable1.getColumnModel().getColumnCount(); i++) {
//                TableColumn selColumn = jTable1.getColumnModel().getColumn(i);
//                String columnHeader = (String) selColumn.getHeaderValue();
//
//                if (columnHeader.equals("分公司")) {
//                    tableNameColumn = i;
//                    break;
//                }
//            }
//            /**
//             * 取得表名并弹出菜单
//             */
//            if (tableNameColumn != -1) {
//                /**
//                 * 修改菜单首条的名称
//                 */
//                String tableName = (String) jTable1.getValueAt(row, tableNameColumn);
//                tableNameItem.setText(tableName);
//                // 弹出菜单
//                popupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
//            }
//        }
    }//GEN-LAST:event_jTable1MousePressed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem10ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;


                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Main4TXTDataJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Main4TXTDataJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Main4TXTDataJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Main4TXTDataJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new Main4TXTDataJFrame().setVisible(true);


                } catch (FileNotFoundException ex) {
                    Logger.getLogger(Main4TXTDataJFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(Main4TXTDataJFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(Main4TXTDataJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables

    /**
     * 设置JTable的值
     *
     * @param ciList
     */
    private void setJTableData(List<ContactInfo> ciList) {

        //把获取到的listciTemp 设置到jTable1中去
        ContactInfo ci = new ContactInfo();
        DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();
        int rowcount = tableModel.getRowCount();
        for (int i = 0; i < rowcount; i++) {
            //清空原来数据
            tableModel.removeRow(0);
        }

        int cilen = ciList.size();
        int i = 0;
        for (int j = 0; j < ciList.size(); j++) {
            ContactInfo citmp = ciList.get(j);
            String[] rowData = new String[]{
                citmp.getCompany(),
                citmp.getDepartment(),
                citmp.getName(),
                citmp.getTel(),
                citmp.getInside(),
                citmp.getFax(),
                citmp.getMobile(),
                citmp.getEmail(),
                citmp.getRemark()
            };
            tableModel.insertRow(i++, rowData);
        }
        //补齐空的JTable内容
        if (ciList.size() < 12) {
            for (int k = ciList.size(); k < 12; k++) {
                String[] rowData = new String[]{
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    ""};
                tableModel.insertRow(k, rowData);
            }
        }

        jTable1.clearSelection();

    }

    /**
     * 设置 登陆管理 退出管理 MenuItem4的文本设置
     *
     * @param title
     */
    public void setJMenuItem4Text(String title) {
        jMenuItem4.setText(title);
    }

    /**
     * JTable1的右键菜单
     */
    private void initJTablePopupMenu() {
        //为Table增加右键菜单
        final JPopupMenu jPopupMenu1 = new JPopupMenu();

        JMenuItem detailItem = new JMenuItem("查看");
        JMenuItem editItem = new JMenuItem("编辑");
        JMenuItem deleteItem = new JMenuItem("删除");

//        jPopupMenu1.addSeparator();
        jPopupMenu1.add(detailItem);
        jPopupMenu1.add(editItem);
        jPopupMenu1.add(deleteItem);
//        jPopupMenu1.addSeparator();


        jTable1.addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showMenu(e);
                }
            }

            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showMenu(e);
                }
            }

            private void showMenu(MouseEvent e) {
                int row = jTable1.getSelectedRow();
                String company = null;
                if (row != -1) {
                    company = (String) jTable1.getValueAt(row, 0);
                }

                if (jTable1.getSelectedRowCount() > 0 && StringUtil.isNotNull(company) && CommonSession.user != null) {
                    jPopupMenu1.show(e.getComponent(), e.getX(), e.getY());
                }

            }
        });

        //查看那右键
        detailItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                int row = jTable1.getSelectedRow();
                String company = (String) jTable1.getValueAt(row, 0);
                String department = (String) jTable1.getValueAt(row, 1);
                String name = (String) jTable1.getValueAt(row, 2);
                String tel = (String) jTable1.getValueAt(row, 3);
                String inside = (String) jTable1.getValueAt(row, 4);
                String fax = (String) jTable1.getValueAt(row, 5);
                String mobile = (String) jTable1.getValueAt(row, 6);
                String email = (String) jTable1.getValueAt(row, 7);
                String remark = (String) jTable1.getValueAt(row, 8);

                ContactInfo ci = new ContactInfo();
                ci.setCompany(company);
                ci.setDepartment(department);
                ci.setName(name);
                ci.setTel(tel);
                ci.setInside(inside);
                ci.setFax(fax);
                ci.setMobile(mobile);
                ci.setEmail(email);
                ci.setRemark(remark);

                //弹出框
                if (StringUtil.isNotNull(company)) {
                    DataDetailFrame ddf = new DataDetailFrame(ci);
                    ddf.setVisible(true);
                }
            }
        });

        //编辑
        editItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                int row = jTable1.getSelectedRow();
                String company = (String) jTable1.getValueAt(row, 0);
                String department = (String) jTable1.getValueAt(row, 1);
                String name = (String) jTable1.getValueAt(row, 2);
                String tel = (String) jTable1.getValueAt(row, 3);
                String inside = (String) jTable1.getValueAt(row, 4);
                String fax = (String) jTable1.getValueAt(row, 5);
                String mobile = (String) jTable1.getValueAt(row, 6);
                String email = (String) jTable1.getValueAt(row, 7);
                String remark = (String) jTable1.getValueAt(row, 8);

                ContactInfo ci = new ContactInfo();
                ci.setCompany(company);
                ci.setDepartment(department);
                ci.setName(name);
                ci.setTel(tel);
                ci.setInside(inside);
                ci.setFax(fax);
                ci.setMobile(mobile);
                ci.setEmail(email);
                ci.setRemark(remark);

                //弹出框
                if (StringUtil.isNotNull(company)) {
                    DataEditFrame def = new DataEditFrame(ci,jTable1,listContactInfo);
                    def.setVisible(true);
                }
            }
        });

        //删除
        deleteItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
//                System.out.println("delete Item");
                String message = "确定要删除该条记录？";
                int res = javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,
                        message, "提示", javax.swing.JOptionPane.OK_CANCEL_OPTION);
//            System.out.println("res:" + res);
                if (res == 0) {
                    //确定
                    int row = jTable1.getSelectedRow();
                    DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();
                    tableModel.removeRow(row);
                }
            }
        });

        //Jtable单元格的默认文本编辑器   
//    DefaultCellEditor objectDefaultCellEditor = new DefaultCellEditor(new MyJTextField()); 
//    jTable1.setDefaultEditor(Object.class,objectDefaultCellEditor);//为Object类型指定默认编辑器  


    }
    
    
     /**
     * 
     * @return 
     */
    public JMenuItem getJMenuItem7(){
        return jMenuItem7;
    }
    /**
     * 
     * @return 
     */
    public JMenuItem getJMenuItem8(){
        return jMenuItem8;
    }
    /**
     * 
     * @return 
     */
    public JMenuItem getJMenuItem10(){
        return jMenuItem10;
    }
}
